const spawn = require('child_process').spawn;

let shell = {
  spawn(path, cmd, parameters = []) {
    return new Promise((resolve, reject) => {
      let stdout = "", stderr = "";
      const worker = spawn(cmd, parameters, {
        cwd: path
      });
      worker.stdout.on('data', (data) => {
        stdout += data;
      });
      worker.stderr.on('data', (data) => {
        stderr += data;
      });
      worker.on('close', (code) => {
        if (code !== 0) {
          reject({code, stderr});
        } else {
          resolve(stdout);
        }
      });
    });
  }
};

module.exports = shell;
